﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Krile.Forms.Dialogs.Popup
{
    public partial class Postbox : Form
    {
        public Postbox()
        {
            InitializeComponent();
        }

        private void Postbox_Load(object sender, EventArgs e)
        {
            Inputter.ShowChangeVLocButton = false;
            Inputter.OnCloseRequired += new EventHandler(Inputter_OnCloseRequired);
            Inputter.Independent = true;
            Inputter.OnTitleMouseDown += new MouseEventHandler(Inputter_OnTitleMouseDown);
            Inputter.OnTitleMouseMove += new MouseEventHandler(Inputter_OnTitleMouseMove);
        }

        void Inputter_OnSelectedServicesChanged(object sender, EventArgs e)
        {
            var svcids = from s in Inputter.SelectedServices
                         select s.Id;
            Core.Config.State.PostBoxLinkedServices = svcids.ToArray<string>();
        }

        Point prevPoint = new Point();
        void Inputter_OnTitleMouseDown(object sender, MouseEventArgs e)
        {
            if((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                prevPoint = e.Location;
            }
        }

        void Inputter_OnTitleMouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                this.Left += e.X - prevPoint.X;
                this.Top += e.Y - prevPoint.Y;
            }
        }

        void Inputter_OnCloseRequired(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Postbox_Shown(object sender, EventArgs e)
        {
            Inputter.Init();
            Inputter.ServiceInit();
            if (Core.Config.State.PostBoxLinkedServices != null)
            {
                var svcs = from sid in Core.Config.State.PostBoxLinkedServices
                           where Core.PluginManager.LookupService(sid) != null
                           select Core.PluginManager.LookupService(sid);
                Inputter.SelectedServices = svcs.ToArray<Krile.Kernel.Data.Service>();
            }
            Inputter.OnSelectedServicesChanged += new EventHandler(Inputter_OnSelectedServicesChanged);
            Inputter.FocusToInput();
        }
    }
}
